//
//  WorkDetailsViewController.m
//  InviteJob
//
//  Created by chirui on 2017/10/24.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "WorkDetailsViewController.h"
#import "CompanyDetailsViewController.h"
#import "TeamDetailsViewController.h"
#import "ReportAlertTVC.h"
#import "MyResumeListViewController.h"

@interface WorkDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{

    UITableView * _tableView;
    NSDictionary * dic;
    UIView * bageView;
    ReportAlertTVC * reportAlert;
    UIButton *_buttonCollect;
}

@property (nonatomic, strong) SZTextView *contentTextView;

@end

@implementation WorkDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"职务详情");
    self.view.backgroundColor =[UIColor whiteColor];
    [self creatTableView];
    [self creatEndView];
    [self getDetailsRequest];
    ;
}

-(void)navButton{
    _buttonCollect = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _buttonCollect.frame =CGRectMake(0, 0, 40, 40);
    [_buttonCollect addTarget:self action:@selector(collectBtn:) forControlEvents:UIControlEventTouchUpInside];
    _buttonCollect.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, -20);
    
    UIBarButtonItem * collectBtn = [[UIBarButtonItem alloc]initWithCustomView:_buttonCollect];
    
    UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [reportButton setTitle:@"举报" forState:UIControlStateNormal];
    reportButton.frame =CGRectMake(0, 0, 40, 40);
    reportButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
    [reportButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [reportButton addTarget:self action:@selector(reportBtn:) forControlEvents:UIControlEventTouchUpInside];
    reportButton.titleLabel.font = [UIFont systemFontOfSize:16];
    UIBarButtonItem * reportBtn = [[UIBarButtonItem alloc]initWithCustomView:reportButton];
    
    self.navigationItem.rightBarButtonItems =@[reportBtn,collectBtn];
}


-(void)collectBtn:(UIButton *)btn{

    [self collectRequest];
}

-(void)reportBtn:(UIButton *)btn{
    isUser
    if (!user.user_id) {
        SHuiMBhudTextTimeStr(@"请先登录");
        return;
    }
    [self roomeReportView];
}

-(void)creatEndView{

    UIButton * endBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    endBtn.frame = CGRectMake(0, HEIGHT- 48 -64, WIDTH, 48);
    endBtn.backgroundColor = NavBackColor;
    [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [endBtn setTitle:@"投递简历" forState:UIControlStateNormal];
    [endBtn addTarget:self action:@selector(OnClickEnd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:endBtn];
}

#pragma mark -- 投递简历
-(void)OnClickEnd {
    isUser
    if (!user.user_id) {
        SHuiMBhudTextTimeStr(@"请先登录");
        return;
    }
    MyResumeListViewController *resume = [MyResumeListViewController new];
    resume.workIdStr = self.workIdStr;
    [self.navigationController pushViewController:resume animated:YES];
     
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-48) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = strColor(@"f6f6f6");
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
  }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (dic) {
        NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",dic[@"content"]]];
        
        
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:7];
        [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [[NSString stringWithFormat:@"%@",dic[@"content"]] length])];
        UILabel * workDetailsLabel = [self creatLabelFrame:CGRectMake(10, 225,WIDTH -20, 20) andTextColor:RGB(0x333333) andFount:15 andTextStr:@"" andAlignment:NSTextAlignmentLeft onView:nil];
        workDetailsLabel.attributedText = attributedString1;
        workDetailsLabel.numberOfLines = 0;
        [workDetailsLabel sizeToFit];
 
        return CGRectGetMaxY(workDetailsLabel.frame)+125;
    }else {
    
        return 150;
    
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static  NSString * str = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }else {
        while ([cell.contentView.subviews lastObject]!=nil) {
            [[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 500)];
    heardView.backgroundColor = [UIColor whiteColor];
    
    
//    NSTextAlignmentLeft     NSTextAlignmentRight
    if (dic) {
    UILabel * workName = [self creatLabelFrame:CGRectMake(10, 15, 30, 20) andTextColor:[UIColor blackColor] andFount:16 andTextStr:dic[@"cate_name"] andAlignment:NSTextAlignmentLeft onView:heardView];
    workName.numberOfLines =1;
    [workName sizeToFit];
    
//    [self creatTagLabelFrame:CGRectMake(CGRectGetMaxX(workName.frame)+5, 15, 20, 14) andText:@" 中介 " onView:heardView];
    
    
    [self creatNoNameLabelFrame:CGRectMake(10, 45,200, 20) andTextColor:RGB(0x999999) andFount:14 andTextStr:[NSString stringWithFormat:@"%@ %@ %@",dic[@"pos_type"],dic[@"work_experience"],dic[@"education"]] andAlignment:NSTextAlignmentLeft onView:heardView];
    
   [self creatNoNameLabelFrame:CGRectMake(WIDTH - 100, 15,90, 20) andTextColor:NavBackColor andFount:16 andTextStr:dic[@"salary"] andAlignment:NSTextAlignmentRight onView:heardView];
    [self creatNoNameLabelFrame:CGRectMake(WIDTH - 130, 45,120, 20) andTextColor:RGB(0x999999) andFount:14 andTextStr:dic[@"update_time"] andAlignment:NSTextAlignmentRight onView:heardView];
    [self creatLineViewFrame:CGRectMake(0, 80, WIDTH, 0.5)  onView:heardView]; // 分割线
    
    UIImageView * companyImage =[[UIImageView alloc]initWithFrame:CGRectMake(10, 90, 50, 50)];
    [companyImage sd_setImageWithURL:ImageUrl(dic[@"company_logo"]) placeholderImage:ImageName(@"")];
    companyImage.layer.cornerRadius = 25.f;
    companyImage.layer.masksToBounds = YES;
    [heardView addSubview:companyImage];
    
    
        
        
        if ([dic[@"type"] isEqualToString:@"company"]) {
            
            UILabel * comLabel = [self creatLabelFrame:CGRectMake(65, 95,30, 20) andTextColor:RGB(0x333333) andFount:14 andTextStr:dic[@"company_name"] andAlignment:NSTextAlignmentLeft onView:heardView];
            comLabel.numberOfLines = 1;
            [comLabel sizeToFit];
            
            [self creatTagLabelFrame:CGRectMake(CGRectGetMaxX(comLabel.frame)+5,95, 20, 14) andText:[NSString stringWithFormat:@" %@ ",dic[@"com_type"]] onView:heardView];
            
            [self creatNoNameLabelFrame:CGRectMake(65, 120,200, 20) andTextColor:RGB(0x999999) andFount:14 andTextStr:dic[@"staff_number"] andAlignment:NSTextAlignmentLeft onView:heardView];
        }else {
        
            UILabel * comLabel = [self creatLabelFrame:CGRectMake(65, 105,30, 20) andTextColor:RGB(0x333333) andFount:14 andTextStr:dic[@"username"] andAlignment:NSTextAlignmentLeft onView:heardView];
            comLabel.numberOfLines = 1;
            [comLabel sizeToFit];
            
            [self creatTagLabelFrame:CGRectMake(CGRectGetMaxX(comLabel.frame)+5,105, 20, 14) andText:[NSString stringWithFormat:@" %@ ",dic[@"com_type"]] onView:heardView];

        }
    UIImageView * moreImage =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH -40, 100, 30, 30)];
    moreImage.image = ImageName(@"icon_user_list_more");
    [heardView addSubview:moreImage];
    
    UIView * tapView =[[UIView alloc]initWithFrame:CGRectMake(0, 80, WIDTH, 70)];
    tapView.backgroundColor =[UIColor clearColor];
    [tapView tapAction:^{
        
        if([dic[@"type"] isEqualToString:@"company"]){
            CompanyDetailsViewController * company =[[CompanyDetailsViewController alloc]init];
            company.company_idStr = dic[@"company_id"];
            [self.navigationController pushViewController:company animated:YES];
        
        }else{
            
          TeamDetailsViewController * team =[[TeamDetailsViewController alloc]init];
          team.company_idStr = dic[@"company_id"];
          [self.navigationController pushViewController:team animated:YES];
        }
    }];
    [heardView addSubview:tapView];
    
    [self creatLineViewFrame:CGRectMake(0, 150, WIDTH, 10)  onView:heardView]; // 分割线
    
    UIView * blueView =[[UIView alloc]initWithFrame:CGRectMake(5, 177.5, 3, 15)];
    blueView.backgroundColor = NavBackColor;
    [heardView addSubview:blueView];
    
    [self creatNoNameLabelFrame:CGRectMake(15, 175,200, 20) andTextColor:[UIColor blackColor] andFount:16 andTextStr:@"职务描述" andAlignment:NSTextAlignmentLeft onView:heardView];
    
    [self creatLineViewFrame:CGRectMake(0, 210, WIDTH, 0.5)  onView:heardView]; // 分割线
    
   
    
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",dic[@"content"]]];
    
    
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:7];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [[NSString stringWithFormat:@"%@",dic[@"content"]] length])];
    
    UILabel * workDetailsLabel = [self creatLabelFrame:CGRectMake(10, 225,WIDTH -20, 20) andTextColor:RGB(0x333333) andFount:15 andTextStr:@"" andAlignment:NSTextAlignmentLeft onView:heardView];
    workDetailsLabel.attributedText = attributedString1;
    
    workDetailsLabel.numberOfLines = 0;
    [workDetailsLabel sizeToFit];
    
    
    [self creatLineViewFrame:CGRectMake(0, CGRectGetMaxY(workDetailsLabel.frame)+15, WIDTH, 10)  onView:heardView]; // 分割线
    
    
    
    UIView * endView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(workDetailsLabel.frame)+25, WIDTH, 100)];
    [heardView addSubview:endView];
    
    UIView * blueView_two =[[UIView alloc]initWithFrame:CGRectMake(5, 17.5, 3, 15)];
    blueView_two.backgroundColor = NavBackColor;
    [endView addSubview:blueView_two];
    
    [self creatNoNameLabelFrame:CGRectMake(15, 15,200, 20) andTextColor:[UIColor blackColor] andFount:16 andTextStr:@"工作地址" andAlignment:NSTextAlignmentLeft onView:endView];
    
    [self creatLineViewFrame:CGRectMake(0, 50, WIDTH, 0.5)  onView:endView];
    
    UIImageView * addImage =[[UIImageView alloc]initWithFrame:CGRectMake(10, 67, 16, 16)];
    addImage.image = ImageName(@"icon_details_address");
    [endView addSubview:addImage];
    
     [self creatNoNameLabelFrame:CGRectMake(30, 65,WIDTH - 40, 20) andTextColor:[UIColor grayColor] andFount:15 andTextStr:dic[@"province_name"] andAlignment:NSTextAlignmentLeft onView:endView];
    }
    return heardView;
}

//举报弹出框
-(void)roomeReportView {
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    bageView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bageView.backgroundColor =[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
    [window addSubview:bageView];
    
    reportAlert = [[[NSBundle mainBundle] loadNibNamed:@"ReportAlertTVC" owner:nil options:nil] lastObject];
    
    self.contentTextView = [[SZTextView alloc]initWithFrame:CGRectMake(10, 60, 260, 95)];
    self.contentTextView.font = [UIFont systemFontOfSize:15];
    self.contentTextView.placeholder = @"请输入举报内容";
    [reportAlert.bgView addSubview:self.contentTextView];
    
    reportAlert.center = CGPointMake(bageView.center.x, self.view.center.y-64);
    
    [reportAlert.surebtn addTarget:self action:@selector(alertSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [reportAlert.closeBtn addTarget:self action:@selector(alertCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [reportAlert.reportBtn addTarget:self action:@selector(alertReportBtn:) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:reportAlert];
   
}

-(void)alertSureBtn:(UIButton *)btn{

    [bageView removeFromSuperview];
    if(self.contentTextView.text.length==0||self.contentTextView.text==nil){
        
        SHuiMBhudTextTimeStr(@"请输入举报内容");
        return;
    }else {
        
        [self reportMessageDetailsRequest];
    }
}

-(void)alertCloseBtn:(UIButton *)btn{
    
    [bageView removeFromSuperview];
}

-(void)alertReportBtn:(UIButton *)btn{
    
    [bageView removeFromSuperview];
    
    self.contentTextView.text = @"举报该公司为中介！";
    [self reportMessageDetailsRequest];
}


-(void)reportMessageDetailsRequest{
    
    isUser
    if (!user.user_id) {
        SHuiMBhudTextTimeStr(@"请先登录");
        return;
    }
    //    1、招聘，2、建企服务，3、简历
    NSDictionary * datadic = @{@"ser_id":@"id",
                               @"user_id":user.user_id,
                               @"token":user.token,
                               @"type":@"1",
                               @"content":self.contentTextView.text
                               };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(Report_request_api) andparameters:datadic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            SHuiMBhudTextTimeStr(@"举报成功");
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        //        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}

-(void)getDetailsRequest{
    
    isUser
    if (!user.user_id) {
        user.user_id = @"";
    }
    NSDictionary * datadic = @{@"zp_id":self.workIdStr,
                               @"user_id":user.user_id};
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(main_jobDetails_api) andparameters:datadic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            dic = responseObject[@"datas"];
            
            if (!dic[@"zp_id"]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            
            [self navButton];
            if ([dic[@"if_collect"] isEqualToString:@"yes"]) {
                [_buttonCollect setImage:[UIImage imageNamed:@"icon_nav_collection"] forState:UIControlStateNormal];
            }else{
                [_buttonCollect setImage:[UIImage imageNamed:@"icon_nav_collection_pre"] forState:UIControlStateNormal];
            }
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}

-(void)collectRequest{
    
    isUser
    if (!user.user_id) {
        SHuiMBhudTextTimeStr(@"请先登录");
        return;
    }
    NSDictionary * datadic = @{@"zp_id":self.workIdStr,
                               @"user_id":user.user_id,
                               @"token":user.token
                               };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(main_jobCollect_api) andparameters:datadic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            if ([dic[@"if_collect"] isEqualToString:@"yes"]) {
                SHuiMBhudTextTimeStr(@"取消收藏成功！");
                [_buttonCollect setImage:[UIImage imageNamed:@"icon_nav_collection_pre"] forState:UIControlStateNormal];
            }else{
                SHuiMBhudTextTimeStr(@"收藏成功！");
                [_buttonCollect setImage:[UIImage imageNamed:@"icon_nav_collection"] forState:UIControlStateNormal];
            }
            
            [self getDetailsRequest];
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
//        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}


-(UILabel *)creatLabelFrame:(CGRect)rec andTextColor:(UIColor *)col andFount:(CGFloat)fount andTextStr:(NSString *)str andAlignment:(NSTextAlignment)alignment onView:(UIView *)view{
    
    UILabel * label =[[UILabel alloc]initWithFrame:rec];
    label.font =[UIFont systemFontOfSize:fount];
    label.textColor =col;
    label.text = str;
    label.textAlignment = alignment;
    [view addSubview:label];
    return label;
}

-(void)creatTagLabelFrame:(CGRect)rec andText:(NSString *)str onView:(UIView *)view{

    UILabel * tag_one =[[UILabel alloc]initWithFrame:rec];
    tag_one.backgroundColor =[UIColor orangeColor];
    tag_one.textColor =[UIColor whiteColor];
    tag_one.textAlignment = NSTextAlignmentCenter;
    tag_one.font = [UIFont systemFontOfSize:14];
    tag_one.layer.cornerRadius = 4.f;
    tag_one.layer.masksToBounds = YES;
    tag_one.text = str;
    tag_one.numberOfLines = 1;
    [tag_one sizeToFit];
    [view addSubview:tag_one];
}

-(void)creatNoNameLabelFrame:(CGRect)rec andTextColor:(UIColor *)col andFount:(CGFloat)fount andTextStr:(NSString *)str andAlignment:(NSTextAlignment)alignment onView:(UIView *)view{
    
    UILabel * label =[[UILabel alloc]initWithFrame:rec];
    label.font =[UIFont systemFontOfSize:fount];
    label.textColor =col;
    label.text = str;
    label.textAlignment = alignment;
    [view addSubview:label];
}

-(void)creatLineViewFrame:(CGRect)rec onView:(UIView *)view{
    
    UIView * lineView = [[UIView alloc]initWithFrame:rec];
    lineView.backgroundColor = strColor(@"f6f6f6");
    [view addSubview:lineView];
}



@end
